package com.atguigu.gulimall.order.listener;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;


@RabbitListener(queues = "order.release.order.queue")
@Service
public class OrderReleaseListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void orderReleaseHandle(Message message, Channel channel, OrderEntity order) throws IOException {
        System.out.println("收到下订单时发送的消息，准备检查并解锁库存");
        try {
            //TODO 取消订单前可以先给支付宝的发一个关闭支付的请求，防止订单关闭后用户进行支付
            orderService.releaseOrder(order);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
            throw new RuntimeException(e);
        }

    }


}
